package com.iraeta.service.basic.impl;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.iraeta.common.ServiceException;
import com.iraeta.dao.main.ErpCurrentStoreDao;
import com.iraeta.entity.main.ErpCurrentStore;
import com.iraeta.entity.main.type.ExcelSheetBean;
import com.iraeta.entity.main.vo.ErpCurrentStoreVo;
import com.iraeta.service.basic.CurrentStoreService;
import com.iraeta.uitl.ExcelUtil;
import com.iraeta.uitl.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CurrentStoreServiceImpl implements CurrentStoreService {
    @Autowired
    ErpCurrentStoreDao erpCurrentStoreDao;
    private static final TimedCache<String, List<String>> whCache = CacheUtil.newTimedCache(30 * 60 * 1000);

    @Override
    public List<ErpCurrentStore> listErpCurrentStoreForPrint(ErpCurrentStoreVo erpCurrentStoreVo) {
        return erpCurrentStoreDao.selectByConditionForPrint(erpCurrentStoreVo);
    }

    @Override
    public List<ErpCurrentStore> listErpCurrentStore(ErpCurrentStoreVo erpCurrentStoreVo) {
        return erpCurrentStoreDao.selectByCondition(erpCurrentStoreVo);
    }

    @Override
    public ErpCurrentStore getErpCurrentStore(Long id) {
        return erpCurrentStoreDao.selectByPrimaryKey(id);
    }

    @Override
    public void updatePdByPrimaryKey(ErpCurrentStore erpCurrentStore) {
        erpCurrentStoreDao.updatePdByPrimaryKey(erpCurrentStore);
    }

    @Override
    public void clearKc(String cwhname) {
        erpCurrentStoreDao.clearKc(cwhname);
    }

    @Override
    public void clearSelectKc(JSONArray selectedAutoIdArray) {
        String ids = Util.parseJSONArrayToString(selectedAutoIdArray);
        erpCurrentStoreDao.clearSelectKc(ids);
    }

    @Override
    public void addPrintCount(JSONArray idsJa) {
        int len = idsJa.size();
        for(int i = 0; i<len;i++){
            Long autoId = idsJa.getLong(i);
            erpCurrentStoreDao.addPrintCount(autoId);
        }
    }

    @Override
    public void exportErpCurrentStore(String filePath, List<ErpCurrentStore> ErpCurrentStoreList) {
        List<String> titles = new ArrayList<>();
        titles.add("仓库名称");
        titles.add("存货编码");
        titles.add("存货名称");
        titles.add("规格型号");
        //titles.add("批号");
        titles.add("单位名称");
        titles.add("盘点时现存量");
        titles.add("盘点数量");
        titles.add("盘亏盘盈");
        titles.add("盘点人员");
        titles.add("盘点时间");
        Map<String, List<ErpCurrentStore>> ErpCurrentStoreGroup = ErpCurrentStoreList.stream().collect(Collectors.groupingBy(ErpCurrentStore::getCwhname));
        List<ExcelSheetBean> excelSheetBeanList = new ArrayList<>();
        for(String cwhName : ErpCurrentStoreGroup.keySet()){
            ExcelSheetBean excelSheetBean = new ExcelSheetBean();
            excelSheetBean.setSheetName(cwhName);
            excelSheetBean.setTitles(titles);
            List<List<String>> contents = new ArrayList<>();
            List<ErpCurrentStore> cwhErpCurrentStores = ErpCurrentStoreGroup.get(cwhName);
            for (ErpCurrentStore cwhErpCurrentStore : cwhErpCurrentStores) {
                List<String> content = new ArrayList<>();
                String czquantity = "";
                String iquantity = "";
                if(cwhErpCurrentStore.getCzquantity() != null){
                    if(cwhErpCurrentStore.getCzquantity().compareTo(new BigDecimal("0")) != 0){
                        czquantity =  cwhErpCurrentStore.getCzquantity().stripTrailingZeros().toPlainString();
                    }else{
                        czquantity = "0";
                    }
                    if(cwhErpCurrentStore.getPdquantity() != null ){
                        iquantity = cwhErpCurrentStore.getPdquantity().subtract(cwhErpCurrentStore.getCzquantity()).stripTrailingZeros().toPlainString();
                    }
                }else{
                    iquantity = cwhErpCurrentStore.getIquantity() == null ? "" : cwhErpCurrentStore.getIquantity().stripTrailingZeros().toPlainString();
                }
                content.add(cwhErpCurrentStore.getCwhname()  == null ? "" : cwhErpCurrentStore.getCwhname());
                content.add(cwhErpCurrentStore.getCinvcode()  == null ? "" : cwhErpCurrentStore.getCinvcode());
                content.add(cwhErpCurrentStore.getCinvname()  == null ? "" : cwhErpCurrentStore.getCinvname());
                content.add(cwhErpCurrentStore.getCinvstd() == null ? "" : cwhErpCurrentStore.getCinvstd());
                //content.add(cwhErpCurrentStore.getCbatch() == null ? "" : cwhErpCurrentStore.getCbatch());
                content.add(cwhErpCurrentStore.getCcomunitname() == null ? "" : cwhErpCurrentStore.getCcomunitname());
                //content.add(cwhErpCurrentStore.getIquantity() == null ? "" : cwhErpCurrentStore.getIquantity().stripTrailingZeros().toPlainString());
                content.add(iquantity);
                content.add(cwhErpCurrentStore.getPdquantity() == null ? "" : cwhErpCurrentStore.getPdquantity().stripTrailingZeros().toPlainString());
                content.add(czquantity);
                content.add(cwhErpCurrentStore.getPduser() == null ? "" : cwhErpCurrentStore.getPduser());
                content.add(cwhErpCurrentStore.getLasttbdate() == null ? "" : DateUtil.formatDateTime(cwhErpCurrentStore.getLasttbdate()));
                contents.add(content);
            }
            excelSheetBean.setContents(contents);
            excelSheetBeanList.add(excelSheetBean);
        }
        try {
            ExcelUtil.writeExcel(filePath, excelSheetBeanList);
        }catch (IOException e){
            throw new ServiceException("盘点数据写入excel报错");
        }
    }
}
